home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Mac Source / Init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-18  |  6.2 KB  |  230 lines  |  [TEXT/CWIE]

  1. /*
  2. ** File:        Init.c
  3. **
  4. ** Written by:    Bill Hayden
  5. **                Nikol Software
  6. **
  7. ** Copyright © 1995 Nikol Software
  8. ** All rights reserved.
  9. */
  10.  
  11.  
  12. #include    "Init.h"
  13. #include    "AEHandler.h"
  14. #include    "Failure.h"
  15. #include     <Palettes.h>
  16. #include    "Menus.h"
  17. #include    "MacWT.h"
  18.  
  19. #include    "Constants.h"
  20.  
  21.  
  22. static    void    StandardInitialization(short callsToMoreMasters);
  23. static    void    InitToolBox(void);
  24. static    void    StandardMenuSetup(short MBARID, short AppleMenuID);
  25. static    void    PullApplicationToFront(void);
  26.  
  27.  
  28. /*****************************************************************************/
  29.  
  30.  
  31.  
  32.  
  33. static    void    InitToolBox(void)
  34. {
  35.     InitGraf(&qd.thePort);
  36.     InitFonts();
  37.     InitWindows();
  38.     InitMenus();
  39.     TEInit();
  40.     InitDialogs(0L);
  41.     InitCursor();
  42. }
  43.  
  44.  
  45.  
  46. /*****************************************************************************/
  47.  
  48.  
  49.  
  50. static    void    StandardInitialization(short callsToMoreMasters)
  51. {
  52.     InitToolBox();
  53.  
  54.     while (callsToMoreMasters--) MoreMasters();
  55.  
  56.     PullApplicationToFront();
  57. }
  58.  
  59.  
  60.  
  61. /*****************************************************************************/
  62.  
  63.  
  64.  
  65.  
  66. static    void    PullApplicationToFront(void)
  67. {
  68. #define kBroughtToFront 3
  69.  
  70.     EventRecord event;
  71.     short        count;
  72.  
  73.     for (count = 1; count <= kBroughtToFront; count++)
  74.         EventAvail(everyEvent, &event);
  75. }
  76.  
  77.  
  78.  
  79. /*****************************************************************************/
  80.  
  81.  
  82.  
  83.  
  84. void    Initialize(short moreMasters, long minHeap, long minSpace)
  85. {
  86.     long        total, contig, qCPU;
  87.     GDHandle    theGDevice;
  88.     OSErr        err;
  89.  
  90.     StandardInitialization(moreMasters);
  91.         
  92.     /* We used to make a check for memory at this point by examining
  93.     ** ApplLimit, ApplicZone, and StackSpace and comparing that to the minimum
  94.     ** size we told the Finder we needed.  This did not work well because it
  95.     ** assumed too much about the relationship between what we asked
  96.     ** the Finder for and what we would actually get back, as well as how to
  97.     ** measure it.  Instead, we will use an alternate method comprised of
  98.     ** two steps. */
  99.  
  100.     /* It is better to first check the size of the application heap against a
  101.     ** value that you have determined is the smallest heap the application can
  102.     ** reasonably work in.  This number should be derived by examining the
  103.     ** size of the heap that is actually provided by the Finder when the
  104.     ** minimum size requested is used.  The derivation of the minimum size
  105.     ** requested from MultiFinder is described in DTS.Lib.h.  The check should
  106.     ** be made because the preferred size can end up being set smaller than
  107.     ** the minimum size by the user.  This extra check acts to ensure that
  108.     ** your application is starting from a solid memory foundation. */
  109.  
  110.     if ((long)GetApplLimit() - (long)ApplicationZone() < kMinMem)
  111.         Fail(MemError(), __FILE__, __LINE__, TRUE);
  112.  
  113.     /* Next, make sure that enough memory is free for your application to run.
  114.     ** It is possible for a situation to arise where the heap may have been of
  115.     ** required size, but a large scrap was loaded which left too little
  116.     ** memory.  To check for this, call PurgeSpace and compare the result with
  117.     ** a value that you have determined is the minimum amount of free memory
  118.     ** your application needs at initialization.  This number can be derived
  119.     ** several different ways.  One way that is fairly straightforward is to
  120.     ** run the application in the minimum size configuration as described
  121.     ** previously.  Call PurgeSpace at initialization and examine the value
  122.     ** returned.  However, you should make sure that this result is not being
  123.     ** modified by the scrap’s presence.  You can do that by calling ZeroScrap
  124.     ** before calling PurgeSpace.  Make sure to remove that call before
  125.     ** shipping, though. */
  126.  
  127.     PurgeSpace(&total, &contig);
  128.     if (total < minSpace)
  129.         Fail(MemError(), __FILE__, __LINE__, TRUE);
  130.  
  131.     /* The extra benefit to waiting until after the Toolbox Managers have been
  132.     ** initialized to check memory is that we can now give the user an alert
  133.     ** to tell him/her what happened.  Although it is possible that the memory
  134.     ** situation could be worsened by displaying an alert, MultiFinder would
  135.     ** gracefully exit the application with an informative alert if memory
  136.     ** became critical.  Here we are acting more in a preventative manner to
  137.     ** avoid future disaster from low-memory problems. */
  138.  
  139. #if GENERATING68K
  140.  
  141.     // Make sure we have System 7.0 or newer, and a 68020 or newer (on 680x0's only)
  142.     // On PowerPC's, both of these are a given, so we don't waste code checking them.
  143.  
  144.     Gestalt(gestaltSystemVersion, &total);
  145.     Gestalt(gestaltProcessorType, &qCPU);
  146.     total = (total >> 8) & 0xf;
  147.     if ((total < 7) || (qCPU < 3))
  148.         {
  149.         StopAlert(rBadStartAlert, nil);
  150.         ExitToShell();
  151.         }
  152.  
  153. #endif
  154.  
  155.     theGDevice = GetMainDevice();
  156.  
  157.     // We need at least an 8-bit monitor
  158.  
  159.     if ( !HasDepth( theGDevice, 8, 0, 0 ) )
  160.         {
  161.         StopAlert(rBadStartAlert, nil);
  162.         ExitToShell();
  163.         }
  164.  
  165.     // Save the current screen depth so we can restore it upon exit
  166.  
  167.     gScreenDepth = (*(*theGDevice)->gdPMap)->pixelSize;
  168.  
  169.     if ( gScreenDepth < 8 )                    // Set depth to at least 8-bit, but…
  170.         SetDepth( theGDevice, 8, 0, 0 );
  171.  
  172.     if ( gScreenDepth > 16 )                 // …not to more than 16-bit.
  173.         {
  174.         err = SetDepth( theGDevice, 16, 0, 0 );
  175.  
  176.         if (err)
  177.             {
  178.             /*
  179.                 Some video cards, such as my Radius 24STV, support 24/32-bit but not 16-bit.
  180.                 In this case, a paramErr is returned and we must go to 8-bit, which we know
  181.                 will work because the above call to HasDepth(8) did not return an error.
  182.             */
  183.             SetDepth( theGDevice, 8, 0, 0 );
  184.             }
  185.         }
  186.  
  187.     // Save the depth we ended up at
  188.  
  189.     gCurrentDepth = (*(*theGDevice)->gdPMap)->pixelSize;
  190.  
  191.     if (gCurrentDepth == 16)
  192.         {
  193.         gTrueColor = TRUE;
  194.         gSizeOfPixel = 2;
  195.         }
  196.  
  197.     StandardMenuSetup(rMenuBar, mApple);
  198.  
  199.     qd.randSeed = TickCount();
  200. }
  201.  
  202.  
  203.  
  204. /*****************************************************************************/
  205.  
  206.  
  207.  
  208.  
  209. void    StandardMenuSetup(short MBARID, short AppleMenuID)
  210. {
  211.     Handle         menuBar = GetNewMBar(MBARID);            /* Read menus into menu bar. */
  212.     MenuHandle    menu;
  213.     
  214.     
  215.     if (!menuBar)
  216.         Fail(ResError(), __FILE__, __LINE__, TRUE);
  217.  
  218.     SetMenuBar(menuBar);                            /* Install menus. */
  219.     DisposeHandle(menuBar);
  220.     AppendResMenu(GetMenuHandle(AppleMenuID), 'DRVR');    /* Add DA names to Apple menu. */
  221.  
  222.     menu = GetMenuHandle(mFile);
  223.     if (menu)
  224.         SetItemMark(menu, kShowFPS, (gShowFPS) ? checkMark : noMark);
  225.         
  226.     DrawMenuBar();
  227. }
  228.  
  229.  
  230.